//
//  HDEasterEggsCenter.m
//  TestEasterEggs
//
//  Created by Mirko on 15/8/10.
//  Copyright (c) 2015年 Mirko. All rights reserved.
//

#import "HDEasterEggsCenter.h"
#import "HDEasterEggsViewController.h"
#import "HDEasterEggsRegistrationCenter.h"
#import "HexColor.h"
#import "UIImage+Tools.h"
#import "UncaughtExceptionHandler.h"

static HDEasterEggsCenter *centerObject = nil;

@interface HDEasterEggsCenter ()

@property (nonatomic, strong) HDEasterEggsRegistrationCenter *registrationCenter;

@end

@implementation HDEasterEggsCenter

-(void) showEasterEggs;
{
    UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
    [rootVC presentViewController:[self initializeVariable] animated:YES completion:^{
        NSLog(@"Present view controller completion!");
    }];
}

+(instancetype)easterEggsCenter {
    static dispatch_once_t onceTaken;
    dispatch_once(&onceTaken, ^{
        centerObject = [[HDEasterEggsCenter alloc] init];
    });
    
    return centerObject;
}

-(instancetype) init
{
    self = [super init];
    if (self) {
        self.registrationCenter = [[HDEasterEggsRegistrationCenter alloc]init];
        InstallUncaughtExceptionHandler();
    }
    return self;
}

-(void)dealloc{
    centerObject = nil;
}

#pragma mark Member method

-(UINavigationController *) initializeVariable
{
    HDEasterEggsViewController *vc = [[HDEasterEggsViewController alloc] init];
    vc.easterEggsList = self.allObject;
    UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:vc];
    [navigation.navigationBar setTintColor:[UIColor whiteColor]];
    [navigation.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    UIImage *image = [UIImage imageWithColor:[UIColor colorWithHexString:@"0588ed"] size:CGSizeMake(1.0, 1.0)];
    [navigation.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    return navigation;
}

- (void)registerObject:(id)object
{
    [_registrationCenter registerObject:object];
}

- (void)registerJumpViewController:(MCBasicViewController *) viewController, ...
{
    va_list params;
    va_start(params, viewController);
    id arg;
    if (viewController) {
        id prev = viewController;
        [_registrationCenter registerObject:prev];
        while( (arg = va_arg(params,id)) )
        {
            if ( arg ){
                [_registrationCenter registerObject:arg];
            }
        }
        va_end(params);
    }
}

- (NSArray *)allObject {
    return _registrationCenter.classArray;
}

@end
